package com.atguigu.tingshu.order.handler;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author atguigu-mqx
 * @ClassName RedisDelayHandle
 * @description: TODO
 * @date 2024年06月21日
 * @version: 1.0
 */
@Slf4j
@Component
public class RedisDelayHandle {
    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;

    //  开启一个线程； 使用定时任务！
    @PostConstruct
    public void init(){
        new Thread(()->{
            while (true){
                //  从队列中获取数据；
                RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(KafkaConstant.QUEUE_ORDER_CANCEL);
                try {
                    String orderNo = (String) blockingDeque.take();
                    log.info("订单取消定时任务，订单号：{}",orderNo);
                    orderInfoService.orderCancel(Long.parseLong(orderNo));
                }catch (Exception e){
                    e.getMessage();
                }
            }
        }).start();
    }

}
